//
//  FlutterNativeWXSmallPlugin.m
//  Runner
//
//  Created by mac on 2020/3/20.
//  Copyright © 2020 The Chromium Authors. All rights reserved.
//

#import "FlutterNativeWXSmallPlugin.h"
#import "WXApi.h"


@interface FlutterNativeWXSmallPlugin()

@property (nonatomic, strong) UIViewController *controller;
@end

@implementation FlutterNativeWXSmallPlugin

static NSString *const CHANNELNAME = @"com.ahj/wxJumpPlugin";
static NSString *const METHODNAME  = @"jumpSmallProgram";
static NSString *const WXID = @"wx83cf55a5a3baaba0";
static NSString *const UniversalLink = @"https://com.anHuanJia.xinanli/";



/**
 *plugin 注册
 */
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
    
    //注册微信小程序
    [WXApi registerApp:WXID universalLink:UniversalLink];
    
    FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:CHANNELNAME binaryMessenger:[registrar messenger]];
    
    UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;

    FlutterNativeWXSmallPlugin *plugin = [[FlutterNativeWXSmallPlugin alloc]initWithViewController:viewController];
    
    [registrar addMethodCallDelegate:plugin channel:channel];
}

/**
 *flutter端通过通道调用原生方法时会进入以下回调
 */
- (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result{
   //call的属性method是flutter调用原生方法的方法名，我们进行字符串判断然后写入不同的逻辑
            NSString *methodName = call.method;
            NSString *userName = call.arguments[@"name"];
            NSString *path = call.arguments[@"url"];

            if ([methodName isEqualToString:METHODNAME]) {
           
                if (![WXApi isWXAppInstalled]) {
                    // result(@"您没有安装微信");
                    // [self kAlertAction:@"您没有安装微信"];
                    return;
                }
                if (![WXApi isWXAppSupportApi]) {
                    result(@"您的微信版本不支持打开小程序");
                    [self kAlertAction:@"您的微信版本不支持打开小程序"];
                    return;
                }
                
                [self wxApplets:userName Path:path];
                
            }else{
                //调用的方法原生没有对应的处理  抛出未实现的异常
                result(FlutterMethodNotImplemented);
            }
}
///微信小程序
- (void)wxApplets :(NSString *)userName Path:(NSString *)path{
    
    if (path==nil) {
        return;
    }
   
    NSArray *strArr = [path componentsSeparatedByString:@"|"];
    NSString *userNameStr = strArr[1];
    NSString *pathStr = strArr[2];

    
    WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
    
    launchMiniProgramReq.userName = userNameStr;  //拉起的小程序的username,appid为gh_开头的，非wx开头的
    launchMiniProgramReq.path = pathStr;    //拉起小程序页面的可带参路径，不填默认拉起小程序首页
    launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease; //拉起小程序的类型
    [WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) {
        
        NSLog(@"拉起小程序返回%d",success);
    }];
    
}
//-(void)onResp:(BaseResp *)resp
//{
//     if ([resp isKindOfClass:[WXLaunchMiniProgramResp class]])
//     {
////          NSString *string = resp.extMsg;
//          // 对应JsApi navigateBackApplication中的extraData字段数据
//     }
//}
- (instancetype)initWithViewController:(UIViewController *)viewController {
    self = [super init];
    if (self) {
        self.controller = viewController;
    }
    return self;
}
- (void)kAlertAction :(NSString *)message{

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];

    [alertVC addAction:confirmAction];
    
    [self.controller presentViewController:alertVC animated:YES completion:nil];

}

@end
